unit UGeist;

interface

uses SysUtils, Classes, Math, UMapObjekte, UPictureName;

type
  TGeist = class(TMapObject) // 3
  private
    FTouched: boolean;
    function GetAbstand(x1, y1, x2, y2: integer): integer;
    procedure MoveTo(Obj: TMapObject);
    procedure MoveBack();
    procedure SetTouched(const Value: boolean);
  public
    constructor Create(Id: integer); Override;
    function getCollisionAtPixel(Game: TObject; X, Y: integer): integer;
      Override;
    procedure Action(Game: TObject); Override;
    property Touched: boolean read FTouched write SetTouched;
  end;

implementation

uses UGame, UPlayer;
{ TGumba }

procedure TGeist.Action(Game: TObject);
var
  Player: TPlayer;
begin
  if self.Touched then
  begin
    self.MoveBack;
  end
  else
  begin
    // Einzelspieler
    if not(TGame(Game).isServer) and not(TGame(Game).isClient) then
    begin
      Player := TGame(Game).ActivePlayer;

      if GetAbstand(Player.StartX + (Player.Width div 2), Player.StartY +
          (Player.Height div 2), self.StartX + (self.Width div 2),
        self.StartY + (self.Height div 2)) < 200 then
        self.MoveTo(Player);
    end
    // Multispieler und Server
    else if TGame(Game).isServer then
    begin
      Player := TGame(Game).Player1;

      if GetAbstand(Player.StartX + (Player.Width div 2), Player.StartY +
          (Player.Height div 2), self.StartX + (self.Width div 2),
        self.StartY + (self.Height div 2)) < 200 then
        self.MoveTo(Player);

      Player := TGame(Game).Player2;

      if GetAbstand(Player.StartX + (Player.Width div 2), Player.StartY +
          (Player.Height div 2), self.StartX + (self.Width div 2),
        self.StartY + (self.Height div 2)) < 200 then
        self.MoveTo(Player);
    end;
  end;
end;

constructor TGeist.Create(Id: integer);
begin
  self.Id := Id;
  self.Num := 5;
  FPictureName := a_Geist;
  self.affgravity := false;
  self.Touched := True;
end;

function TGeist.GetAbstand(x1, y1, x2, y2: integer): integer;
begin
  result := Round(sqrt(power((x1 - x2), 2) + power((y1 - y2), 2)));
end;

function TGeist.getCollisionAtPixel(Game: TObject; X, Y: integer): integer;
begin
  if Touched then
    result := 1
  else
  begin
    result := 2;
    self.Touched := True;
  end;
end;

procedure TGeist.MoveBack;
begin
  if self.Orig_StartX > self.StartX then
  begin
    inc(self.FStartX);
    inc(self.FEndeX);
  end
  else
  begin
    dec(self.FStartX);
    dec(self.FEndeX);
  end;

  if self.Orig_StartY > self.StartY then
  begin
    inc(self.FStartY);
    inc(self.FEndeY);
  end
  else
  begin
    dec(self.FStartY);
    dec(self.FEndeY);
  end;

  if ((self.Orig_StartX = self.StartX) and (self.Orig_StartY = self.StartY))
    then
    self.Touched := false;

  if Assigned(self.OnMove) then
    self.OnMove(self);
end;

procedure TGeist.MoveTo(Obj: TMapObject);
begin
  if Obj.StartX > self.StartX + 5 then
  begin
    inc(self.FStartX);
    inc(self.FEndeX);
  end
  else
  begin
    dec(self.FStartX);
    dec(self.FEndeX);
  end;

  if Obj.StartY > self.StartY + 5 then
  begin
    inc(self.FStartY);
    inc(self.FEndeY);
  end
  else
  begin
    dec(self.FStartY);
    dec(self.FEndeY);
  end;

  if Assigned(self.OnMove) then
    self.OnMove(self);
end;

procedure TGeist.SetTouched(const Value: boolean);
begin
  FTouched := Value;
end;

end.
